const AppConfig = require('../../configuration');
const { shell, ipcRenderer } = require('electron');

const lan = AppConfig.readSettings('language');
const recall = AppConfig.readSettings('prevent-recall');
const instance = AppConfig.readSettings('multi-instance');
const trayColor = AppConfig.readSettings('tray-color');
const devMode = AppConfig.readSettings('dev-mode');
const logLevel = AppConfig.readSettings('logger-level');
const winSizeWidth = AppConfig.readSettings('window-size:width');
const winSizeHeight = AppConfig.readSettings('window-size:height');
const downloadDir = AppConfig.readSettings('download-dir');

const lanSelect = $('app-language-select');
const recallSelect = $('app-recall-select');
const instanceSelect = $('app-instance-select');
const trayColorSelect = $('app-tray-select');
const devModeSelect = $('dev-mode-select');
const winSizeWidthSelect = $('win-size-width-select');
const winSizeHeightSelect = $('win-size-height-select');
const logLevelSelect = $('logger-level-select');
const downloadDirSelect = $('download-dir-select');

function $(id) {
  return document.getElementById(id);
}

setConfig();
setListeners();

let Common
if(AppConfig.readSettings('language') === 'zh_CN') {
  Common = require('../../common_cn') 
} else {
  Common = require('../../common');
  setLocale()
}

function setListeners() {
  lanSelect.addEventListener('change', function() {
    AppConfig.saveSettings('language', lanSelect.value)
  })
  recallSelect.addEventListener('change', function() {
    AppConfig.saveSettings('prevent-recall', recallSelect.value === "true" ? true : false)
  })
  instanceSelect.addEventListener('change', function() {
    AppConfig.saveSettings('multi-instance', instanceSelect.value === "true" ? true : false)
  })
  trayColorSelect.addEventListener('change', function() {
    AppConfig.saveSettings('tray-color', trayColorSelect.value)
    ipcRenderer.send('refreshIcon')
  })
  devModeSelect.addEventListener('change', function() {
    AppConfig.saveSettings('dev-mode', devModeSelect.value === "true" ? true : false)
  })
  logLevelSelect.addEventListener('change', function() {
    AppConfig.saveSettings('logger-level', logLevelSelect.value)
  })
  winSizeWidthSelect.addEventListener('change', function() {
    AppConfig.saveSettings('window-size:width', parseInt(winSizeWidthSelect.value))
  })
  winSizeHeightSelect.addEventListener('change', function() {
    AppConfig.saveSettings('window-size:height', parseInt(winSizeHeightSelect.value))
  })
  downloadDirSelect.addEventListener('change', function() {
    AppConfig.saveSettings('download-dir', downloadDirSelect.value)
  })
}

function setConfig() {
  $('app-language-select').value = lan;
  $('app-recall-select').value = recall;
  $('app-instance-select').value = instance;
  $('app-tray-select').value = trayColor;
  $('logger-level-select').value = logLevel;
  $('dev-mode-select').value = devMode;
  $('win-size-width-select').value = winSizeWidth;
  $('win-size-height-select').value = winSizeHeight;
  $('download-dir-select').value = downloadDir;
}

function setLocale() {
  $('app-language-title').innerHTML = Common.languageTitle;
  $('app-language-desc').innerHTML = Common.languageDesc;
  $('app-recall-title').innerHTML = Common.recallTitle;
  $('app-recall-desc').innerHTML = Common.recallDesc;
  $('app-instance-title').innerHTML = Common.instanceTitle;
  $('app-instance-desc').innerHTML = Common.instanceDesc;
  $('app-icon-title').innerHTML = Common.iconTitle;
  $('app-icon-desc').innerHTML = Common.iconDesc;
  $('app-tray-title').innerHTML = Common.trayTitle;
  $('app-tray-desc').innerHTML = Common.trayDesc;
  $('dev-mode-title').innerHTML = Common.DevModeTitle;
  $('dev-mode-desc').innerHTML = Common.DevModeDesc;
  $('logger-level-title').innerHTML = Common.LoggerLevelTitle;
  $('logger-level-desc').innerHTML = Common.LoggerLevelDesc;
  $('win-size-title').innerHTML = Common.WinSizeTitle;
  $('win-size-desc').innerHTML = Common.WinSizeDesc;
  $('download-dir-title').innerHTML = Common.DownloadDirTitle;
  $('download-dir-desc').innerHTML = Common.DownloadDirDesc;
  $('upgrade-btn').innerHTML = Common.UPGRADE;
  $('feedback-btn').innerHTML = Common.FEEDBACK;
}

function feedback() {
  shell.openExternal(Common.GITEE_ISSUES);
}

function upgrade() {
  ipcRenderer.send('update');
}

function KeyDownFn(evt) {
  if (evt.keyCode == 73 && evt.ctrlKey && evt.shiftKey) evt.preventDefault();
}
